package com.wd.attend.service.impl;

import java.util.*;

import com.wd.attend.domain.AttendResult;
import com.wd.attend.domain.CalendarData;
import com.wd.attend.domain.DeptAttendReport;
import com.wd.attend.mapper.AttendResultMapper;
import com.wd.common.utils.DateUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.wd.attend.mapper.AttendReportMapper;
import com.wd.attend.domain.AttendReport;
import com.wd.attend.service.IAttendReportService;
import org.springframework.transaction.annotation.Transactional;

import static com.wd.attend.controller.utils.DateTimeUtils.getDay;
import static com.wd.attend.controller.utils.DateTimeUtils.getMonth;

/**
 * 考勤统计Service业务层处理
 * 
 * @author liumingkun
 * @date 2023-11-21
 */
@Service
public class AttendReportService implements IAttendReportService
{
    @Resource
    private AttendReportMapper attendReportMapper;

    @Resource
    private AttendResultMapper resultMapper;

    /**
     * 查询考勤统计
     * 
     * @param reportId 考勤统计主键
     * @return 考勤统计
     */
    @Override
    public AttendReport selectAttendReportByReportId(Long reportId)
    {
        return attendReportMapper.selectAttendReportByReportId(reportId);
    }

    /**
     * 查询考勤统计列表
     * 
     * @param attendReport 考勤统计
     * @return 考勤统计
     */
    @Override
    public List<AttendReport> selectAttendReportList(AttendReport attendReport)
    {
        return attendReportMapper.selectAttendReportList(attendReport);
    }

    /**
     * 查询考勤统计列表
     *
     * @param deptAttendReport 考勤统计
     * @return 考勤统计
     */
    @Override
    public List<DeptAttendReport> selectDeptAttendReport(DeptAttendReport deptAttendReport){
        return attendReportMapper.selectDeptAttendReport(deptAttendReport);
    }

    /**
     * 新增考勤统计
     * 
     * @param attendReport 考勤统计
     * @return 结果
     */
    @Override
    public int insertAttendReport(AttendReport attendReport)
    {
        attendReport.setCreateTime(DateUtils.getNowDate());
        return attendReportMapper.insertAttendReport(attendReport);
    }

    /**
     * 修改考勤统计
     * 
     * @param attendReport 考勤统计
     * @return 结果
     */
    @Override
    public int updateAttendReport(AttendReport attendReport)
    {
        attendReport.setUpdateTime(DateUtils.getNowDate());
        return attendReportMapper.updateAttendReport(attendReport);
    }

    /**
     * 批量删除考勤统计
     * 
     * @param reportIds 需要删除的考勤统计主键
     * @return 结果
     */
    @Override
    public int deleteAttendReportByReportIds(Long[] reportIds)
    {
        return attendReportMapper.deleteAttendReportByReportIds(reportIds);
    }

    /**
     * 删除考勤统计信息
     * 
     * @param reportId 考勤统计主键
     * @return 结果
     */
    @Override
    public int deleteAttendReportByReportId(Long reportId)
    {
        return attendReportMapper.deleteAttendReportByReportId(reportId);
    }


    @Transactional
    @Override
    public void attendReportJob(Integer attendYear,Integer attendMonth)
    {
        attendReportMapper.deleteAttendReport(attendYear,attendMonth);
        Calendar calendar=Calendar.getInstance();
        calendar.set(attendYear,attendMonth-1,1,0,0,0);
        Date startTime = calendar.getTime();
        calendar.add(Calendar.MONTH,1);
        Date endTime = calendar.getTime();
        attendReportMapper.attendReportJob(attendYear,attendMonth,startTime,endTime);
    }

    @Override
    public List<CalendarData> getStaffReport(AttendReport attendReport) {

        AttendResult query=new AttendResult();
        query.setStaffNum(attendReport.getStaffNum());
        List<AttendResult> attendResults = resultMapper.selectAttendResultList(query);
        List<CalendarData> dataList=new ArrayList<>();
        for (AttendResult result: attendResults) {
            CalendarData calendarData = new CalendarData();

            Set<String> months=new HashSet<>();
            months.add(getMonth(result.getAttendDate()));

            Set<String> days=new HashSet<>();
            days.add(getDay(result.getAttendDate()));

            calendarData.setMonths(months);
            calendarData.setDays(days);
            calendarData.setThings(result.getAttendResult());

            dataList.add(calendarData);
        }
        return dataList;
    }
}
